﻿Imports System.ComponentModel
Imports System.Drawing

<Serializable()> Public Class SettingsType

    'Database Settings
    Dim iDbSettings As New DBSettingsType
    Public Property DbSettings As DBSettingsType
        Get
            Return Me.iDbSettings
        End Get
        Set(ByVal value As DBSettingsType)
            Me.iDbSettings = value
        End Set
    End Property

    'Various form settings
    Dim iFormSettings As New FormSettingsTypeCollection
    Public Property FormSettings As FormSettingsTypeCollection
        Get
            Return Me.iFormSettings
        End Get
        Set(value As FormSettingsTypeCollection)
            Me.iFormSettings = value
        End Set
    End Property

End Class

<Serializable()> Public Class DBSettingsType

    'Connection string
    Dim iConnectionString As String = Utility.EncriptData_3DES("Data Source=WAAHID-PC;Initial Catalog=abyx_svcm;Persist Security Info=True;User ID=sa;Password=penTium4")
    Public Property ConnectionString As String
        Get
            Return Me.iConnectionString
        End Get
        Set(value As String)
            Me.iConnectionString = value
            ''to avoid unncessary encryption
            'If Me.iConnectionString.Contains("svcm") Then
            '    Me.iConnectionString = Utility.EncriptData(Me.iConnectionString)
            'End If
        End Set
    End Property

    Public Property DatabaseBackupPath As String = "D:\SQL_Backups"

End Class

<Serializable()> _
<Xml.Serialization.XmlInclude(GetType(Size))> _
<Xml.Serialization.XmlInclude(GetType(Point))> _
Public Class FormSettingsType

    Private iFormName As String
    Public Property FormName As String
        Get
            Return Me.iFormName
        End Get
        Set(value As String)
            Me.iFormName = value
        End Set
    End Property
    Private iControlName As String
    Public Property ControlName As String
        Get
            Return Me.iControlName
        End Get
        Set(value As String)
            Me.iControlName = value
        End Set
    End Property
    Private iPropertyName As String
    Public Property PropertyName As String
        Get
            Return Me.iPropertyName
        End Get
        Set(value As String)
            Me.iPropertyName = value
        End Set
    End Property
    Private iValue As Object
    Public Property Value As Object
        Get
            Return Me.iValue
        End Get
        Set(value As Object)
            Me.iValue = value
        End Set
    End Property

    Public Sub New()

    End Sub
    Public Sub New(FormName As String, _
               ControlName As String, _
               PropertyName As String)
        Me.iFormName = FormName
        Me.iControlName = ControlName
        Me.iPropertyName = PropertyName
    End Sub
    Public Sub New(FormName As String, _
                   ControlName As String, _
                   PropertyName As String, _
                   Value As Object)
        Me.iFormName = FormName
        Me.iControlName = ControlName
        Me.iPropertyName = PropertyName
        Me.iValue = Value
    End Sub
    Public Sub New(Form As Windows.Forms.Form, _
                   Control As Windows.Forms.Control, _
                   PropertyName As String)
        Me.iFormName = Form.Name
        Me.iControlName = Control.Name
        Me.iPropertyName = PropertyName
    End Sub
    Public Sub New(Form As Windows.Forms.Form, _
                   Control As Windows.Forms.Control, _
                   PropertyName As String, _
                   Value As Object)
        Me.iFormName = Form.Name
        Me.iControlName = Control.Name
        Me.iPropertyName = PropertyName
        Me.iValue = Value
    End Sub

    Public Overloads Shared Operator =(ByVal obj1 As FormSettingsType, ByVal obj2 As FormSettingsType) As Boolean
        Return ((obj1.FormName = obj2.FormName) _
                AndAlso (obj1.ControlName = obj2.ControlName) _
                AndAlso (obj1.PropertyName = obj2.PropertyName))
    End Operator
    Public Overloads Shared Operator <>(ByVal obj1 As FormSettingsType, ByVal obj2 As FormSettingsType) As Boolean
        Return (Not (obj1 = obj2))
    End Operator

    Public Overrides Function ToString() As String
        Return Me.iFormName & "." & Me.iControlName & "." & Me.iPropertyName & " (" & Me.iValue.ToString & ")"
    End Function

End Class

<Serializable()> Public Class FormSettingsTypeCollection
    Inherits CollectionBase

    Public Sub New()
        MyBase.New()
    End Sub

    Default Public Property Item(Index As Integer) As FormSettingsType
        Get
            Return Me.List(Index)
        End Get
        Set(value As FormSettingsType)
            Me.List(Index) = value
        End Set
    End Property

    Default Public ReadOnly Property Item(form As Windows.Forms.Form, _
                                          control As Windows.Forms.Control, _
                                          propertyname As String) As FormSettingsType
        Get
            Dim s As New FormSettingsType(form, control, propertyname)
            For Each i As FormSettingsType In Me.List
                If i = s Then Return i
            Next
            Return Nothing
        End Get
    End Property

    Protected NotOverridable Overrides Sub OnValidate(ByVal value As Object)
        If IsNothing(value) Then Exit Sub
        If Not GetType(FormSettingsType).IsAssignableFrom(value.GetType()) Then
            Throw New ArgumentException("Value must be of type FormSettingsType")
        End If
    End Sub

    Protected NotOverridable Overrides Sub OnInsert(ByVal index As Integer, ByVal value As Object)
        MyBase.OnInsert(index, value)
    End Sub

    Protected NotOverridable Overrides Sub OnRemove(ByVal index As Integer, ByVal value As Object)
        MyBase.OnRemove(index, value)
    End Sub

    Protected NotOverridable Overrides Sub OnClear()
        MyBase.OnClear()
    End Sub

    Protected NotOverridable Overrides Sub OnSet(ByVal index As Integer, _
                                                 ByVal oldValue As Object, _
                                                 ByVal newValue As Object)
        MyBase.OnSet(index, oldValue, newValue)
    End Sub

    Public Overridable Function IndexOf(ByVal value As FormSettingsType) As Integer
        'Return Me.List.IndexOf(value)
        If Me.List.Count > 0 Then
            For i As Integer = 0 To Me.List.Count - 1
                Dim s As FormSettingsType = Me.List(i)
                If s = value Then
                    Return i
                End If
            Next
        End If
        Return -1
    End Function

    Public Overridable Function Add(ByVal value As FormSettingsType) As Integer
        Return Me.List.Add(value)
    End Function

    Public Overridable Sub Insert(ByVal index As Integer, ByVal value As FormSettingsType)
        Me.List.Insert(index, value)
    End Sub

    Public Overridable Overloads Sub Remove(ByVal value As FormSettingsType)
        Me.List.RemoveAt(List.IndexOf(value))
        'Dim i As Integer = 0
        'For i = 0 To Me.List.Count - 1
        '    If CType(Me.List.Item(i), FormSettingsType).iId = value.iId Then
        '        Me.List.RemoveAt(i)
        '        Exit For
        '    End If
        'Next
    End Sub

    Public Overridable Function Contains(ByVal value As FormSettingsType) As Boolean
        'Return Me.List.Contains(value)
        For Each Item As FormSettingsType In Me.List
            If Item = value Then
                Return True
            End If
        Next
        Return False
    End Function

End Class

